#include "sdk_project_config.h"
#include "app_etmr.h"
#include "app_adc.h"

volatile int32_t DRV_PWMCOUNT;					/*MOD值*/
static Etmr_Val Pwm_4Way,Pwm_Mode;			/*占空比结构体定义*/
extern volatile uint32_t ADC_MODEVF;		/*ADC采集给模式电机角度反馈*/
uint8_t Flag_4Way;								/*四通阀标志位，1逆时针转，0顺时针转*/
uint8_t Flag_Mode;								/*模式电机标志位，1正常运行，0关闭*/
uint16_t target_angle;
//uint16_t s32arg1,s32arg2;

void app_etmr_Motors_Init(void)
{
	DRV_PWMCOUNT = 0x1D4B;								/*16Khz（16000）时的MOD值（PLL1分频，120MHz/16K = 7500，Hex为1D4B）*/
	Pwm_4Way.s32arg1 = 0;
	Pwm_4Way.s32arg2 = 0;
	Pwm_Mode.s32arg1 = 0;
	Pwm_Mode.s32arg2 = 0;
	Flag_4Way = 2;												/*四通阀标志位，1逆时针转，0顺时针转*/
	Flag_Mode = 2;												/*模式电机标志位，1正常运行，0关闭*/
}


/*四通阀PWM输出*/
void Send_Pwm_4Way(Etmr_Val *pwm)
{
	/*etmr0通道配置，配置0/1通道控制四通阀*/
	eTMR0->CH[0].VAL0 = (DRV_PWMCOUNT>>1)-(pwm->s32arg1>>1);
	eTMR0->CH[0].VAL1 = (DRV_PWMCOUNT>>1)+(pwm->s32arg1>>1);
	eTMR0->CH[1].VAL0 = (DRV_PWMCOUNT>>1)-(pwm->s32arg2>>1);
	eTMR0->CH[1].VAL1 = (DRV_PWMCOUNT>>1)+(pwm->s32arg2>>1);
	
	eTMR0->SYNC &= ~eTMR_SYNC_LDOK_MASK;
	eTMR0->SYNC |= eTMR_SYNC_LDOK_MASK;
}

void Motor_4Way_Driver(void)
{
	/*逆时针转*/
	if(Flag_4Way == 1)
	{
		Pwm_4Way.s32arg1 = 0;
		Pwm_4Way.s32arg2 = DRV_PWMCOUNT;
	}
	/*顺时针转*/	
	else if(Flag_4Way == 0)
	{
		Pwm_4Way.s32arg1 = DRV_PWMCOUNT;
		Pwm_4Way.s32arg2 = 0;
	}
	else
	{
		Pwm_4Way.s32arg1 = 0;
		Pwm_4Way.s32arg2 = 0;
	}
	/*更新占空比*/
	Send_Pwm_4Way(&Pwm_4Way);
}

void Send_Pwm_Mode(Etmr_Val *pwm)
{
	/*etmr1通道配置，配置4/5通道控制模式电机*/
	eTMR1->CH[4].VAL0 = (DRV_PWMCOUNT>>1)-(pwm->s32arg1>>1);
	eTMR1->CH[4].VAL1 = (DRV_PWMCOUNT>>1)+(pwm->s32arg1>>1);
	eTMR1->CH[5].VAL0 = (DRV_PWMCOUNT>>1)-(pwm->s32arg2>>1);
	eTMR1->CH[5].VAL1 = (DRV_PWMCOUNT>>1)+(pwm->s32arg2>>1);
	
	eTMR1->SYNC &= ~eTMR_SYNC_LDOK_MASK;
	eTMR1->SYNC |= eTMR_SYNC_LDOK_MASK;
}
void Mode_Motor_Driver(void)
{
	/*逆时针转*/
	if(Flag_Mode == 1)
	{
		Pwm_Mode.s32arg1 = 0;
		Pwm_Mode.s32arg2 = DRV_PWMCOUNT;
	}
	/*顺时针转*/	
	else if(Flag_Mode == 0)
	{
		Pwm_Mode.s32arg1 = DRV_PWMCOUNT;
		Pwm_Mode.s32arg2 = 0;
	}
	else
	{
		Pwm_Mode.s32arg1 = 0;
		Pwm_Mode.s32arg2 = 0;
	}
	/*更新占空比*/
	Send_Pwm_Mode(&Pwm_Mode);
}

//void Mode_Motor_Driver(void)
//{
//	switch (Flag_Mode)
//	{
//		case 0:
//			target_angle = 0xFE0;//最大角度
//			break;
//		case 1:
//			target_angle = 0x7F0;//中间角度
//			break;
//		case 2:
//			target_angle = 0x030;//最小角度
//			break;
//		default:
//			break;
//	}
//	
//	if(ADC_MODEVF > target_angle)
//	{
//		Pwm_Mode.s32arg1 = 5*(DRV_PWMCOUNT/10);
//		Pwm_Mode.s32arg2 = 9*(DRV_PWMCOUNT/10);
////		s32arg1 = Pwm_Mode.s32arg1;
////		s32arg2 = Pwm_Mode.s32arg2;
//		if((ADC_MODEVF <= (target_angle + 0x00F))&&(ADC_MODEVF >= (target_angle - 0x00F)))//转动电机到该角度上下浮动值
//		{
//			Pwm_Mode.s32arg1 = 0;
//			Pwm_Mode.s32arg2 = 0;
////			s32arg1 = Pwm_Mode.s32arg1;
////			s32arg2 = Pwm_Mode.s32arg2;
//		}
//	}
//	if(ADC_MODEVF < target_angle)
//	{
//		Pwm_Mode.s32arg1 = 9*(DRV_PWMCOUNT/10);
//		Pwm_Mode.s32arg2 = 5*(DRV_PWMCOUNT/10);
////		s32arg1 = Pwm_Mode.s32arg1;
////		s32arg2 = Pwm_Mode.s32arg2;
//		if((ADC_MODEVF <= (target_angle + 0x00F))&&(ADC_MODEVF >= (target_angle - 0x00F)))//转动电机到该角度上下浮动值
//		{
//			Pwm_Mode.s32arg1 = 0;
//			Pwm_Mode.s32arg2 = 0;
////			s32arg1 = Pwm_Mode.s32arg1;
////			s32arg2 = Pwm_Mode.s32arg2;
//		}
//	}
//	else{}
//	/*更新占空比*/
//	Send_Pwm_Mode(&Pwm_Mode);
//}